﻿using MEF_Test.Subtitles;

namespace OpenAction.MydraVideoOpenActionContextProvider
{
    using System;
    using System.Collections.Generic;
    using MEF_Test.Bricks.Subtitles;
    using MEF_Test.Contracts;

    using Microsoft.Practices.Prism.Regions;

    public class MydraVideoOpenActionContextProvider : IOpenActionContextProvider
    {
        #region Constants and Fields

        /// <summary>
        /// A list of all the content types supported by <see cref="MydraVideoOpenActionContextProvider"/>.
        /// </summary>
        private readonly List<string> _supportedContentTypes = new List<string>
            {
                // this could be loaded from an XML file.
                "video/avi",
                "video/matroska",
                "audio/mp3",
                "audio/ogg"
            };

        /// <summary>
        /// The renderer module to use for video rendering.
        /// </summary>
        private readonly IVideoRendererController _videoRenderer;

        /// <summary>
        /// The manager In charge of importing all the SubtitlesProviders.
        /// </summary>
        private readonly ISubtitlesProvidersManager _subtitlesProvidersManager;

        private readonly IRegionManager _regionManager;
        private readonly ISubtitleSelectionViewFactory _subtitleSelectionViewFactory;
        private readonly ISubtitlesSelectionPageViewModelFactory _subtitlesSelectionPageViewModel;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MydraVideoOpenActionContextProvider"/> class.
        /// </summary>
        /// <param name="videoRenderer">The video renderer.</param>
        /// <param name="subtitlesProvidersManager">The subtitles-providers manager.</param>
        /// <param name="regionManager">The region manager.</param>
        /// <param name="subtitleSelectionViewFactory">The subtitle selection view factory.</param>
        /// <param name="subtitlesSelectionPageViewModelFactory">The subtitles selection page view model factory.</param>
        public MydraVideoOpenActionContextProvider(IVideoRendererController videoRenderer, ISubtitlesProvidersManager subtitlesProvidersManager, IRegionManager regionManager, ISubtitleSelectionViewFactory subtitleSelectionViewFactory, ISubtitlesSelectionPageViewModelFactory subtitlesSelectionPageViewModelFactory)
        {
            if (videoRenderer == null)
            {
                throw new ArgumentNullException("videoRenderer");
            }
            if (subtitlesProvidersManager == null)
            {
                throw new ArgumentNullException("subtitlesProvidersManager");
            }
            if (regionManager == null)
            {
                throw new ArgumentNullException("regionManager");
            }
            if (subtitlesSelectionPageViewModelFactory == null)
                throw new ArgumentNullException("subtitlesSelectionPageViewModelFactory");

            _videoRenderer = videoRenderer;
            _subtitlesProvidersManager = subtitlesProvidersManager;
            _regionManager = regionManager;
            _subtitleSelectionViewFactory = subtitleSelectionViewFactory;
            _subtitlesSelectionPageViewModel = subtitlesSelectionPageViewModelFactory;
        }

        #endregion

        #region Implemented Interfaces

        #region IActionContextProvider

        /// <summary>
        /// Checks if the specified node is supported by the <see cref="IOpenActionContextProvider"/>
        /// </summary>
        /// <param name="node">The node for which the support check is needed.</param>
        /// <returns><c>true</c> if the node is supported, <c>false</c> otherwise.</returns>
        public bool Supports(IBrowserNodeViewModel node)
        {
                return _supportedContentTypes.Contains(node.FileHandler.ContentType);
        }

        #endregion

        #region IOpenActionContextProvider

        /// <summary>
        /// Processes the specified node.
        /// </summary>
        /// <param name="node">The node to process.</param>
        public void Process(IBrowserNodeViewModel node)
        {
            _videoRenderer.OpenVideo(node);          
        }

        #endregion

        #endregion
    }
}
